package com.aaa.market.service.impl;


import com.aaa.market.mapper.AdvertiseMapper;
import com.aaa.market.mapper.PositionMapper;
import com.aaa.market.pojo.entity.Advertise;
import com.aaa.market.pojo.vo.AdvVo;
import com.aaa.market.pojo.vo.AdvertiseVo;
import com.aaa.market.service.AdvertiseService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsj.common.pojo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: clms-eighth
 * @description:
 * @author: 刘守霖
 * @create: 2021-07-24 15:42
 **/
@Service
public class AdvertiseServiceImpl implements AdvertiseService {
    @Autowired
    private AdvertiseMapper advertiseMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Override
    public R findAdvertiseByPage(Integer currentPage, Integer pageSize, AdvertiseVo advertiseVo) {
        Page<AdvVo> page = new Page<>(currentPage,pageSize);

        IPage iPage = advertiseMapper.selectPage1(page, advertiseVo);
        return R.ok().put("records",iPage.getRecords()).put("total",iPage.getTotal());
    }

    @Override
    public R insertAdvertise(Advertise advertise) {
        String name = advertise.getAdvertiseName();
        List<Advertise> selectAll = advertiseMapper.selectAll();
        for (Advertise advertise1:selectAll){
            if (advertise1.getAdvertiseName().equals(name)){
                return R.ok().put("message","名字已重复");
            }
        }
        advertiseMapper.insertAdvertiseTable(advertise);
        return R.ok().put("message","新增成功");
    }

    @Override
    public R delAdvertise(Integer id) {
        advertiseMapper.delAdvertise(id);
        return null;
    }

    @Override//修改前获取数据
    public R chaXun(Integer id) {
        List<Advertise> advertises = advertiseMapper.chaXun(id);
        return R.ok().put("advertises",advertises);
    }

    @Override
    public R changeAdvertise(Advertise advertise) {
//        String name = advertise.getName();
//        List<Advertise> selectAll = advertiseMapper.selectAll();
//        for (Advertise advertise1:selectAll){
//            if (advertise1.getName().equals(name)){
//                return R.ok().put("message","名字已重复");
//            }
//
//        }
        advertiseMapper.changeAdvertise(advertise);
        return R.ok().put("message","修改成功");
    }


    @Override //改变状态
    public R changeStatus(Integer id, Integer status) {
        advertiseMapper.changeStatus(id,status);
        return null;
    }

    @Override
    public R queryPic() {
        List<Advertise> queryPic = advertiseMapper.queryPic();
        return R.ok().put("queryPic",queryPic);
    }

}
